package com.atgugu.springcloud.controller;


import cn.hutool.core.bean.BeanUtil;
import com.atgugu.springcloud.entities.Pay;
import com.atgugu.springcloud.entities.PayDTO;
import com.atgugu.springcloud.resp.ResultData;
import com.atgugu.springcloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@Tag(name = "支付微服务模块",description = "支付CURD")
public class PayController {

    @Resource
    private PayService payService;

    @Operation(summary = "新增",description = "新增支付流水方法,json串做参数")
    @PostMapping("/pay/add")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        System.out.println(pay.toString());
         int i =   payService.add(pay);
        return ResultData.success("成功插入记录，返回值："+i);
    }


    @Operation(summary = "删除",description = "删除支付流水方法")
    @DeleteMapping(value="/pay/del/{id}")
    public  ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
       int i = payService.delete(id);
       return ResultData.success(i);
    }

    @Operation(summary = "修改",description = "修改支付流水方法")
    @PutMapping("/pay/update")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        //update更新要传主键，不然会报错并且更新失败
        Pay pay = new Pay();
        BeanUtil.copyProperties(payDTO,pay);
        int i =   payService.update(pay);
        return ResultData.success("成功记录，返回值："+i);
    }

    @Operation(summary = "按照ID查流水",description = "查询支付流水方法")
    @GetMapping("/pay/get/{id}")
    public  ResultData<Pay> getById(@PathVariable("id") Integer id) {
        try {
            TimeUnit.SECONDS.sleep(62);
        }catch (InterruptedException e) {
            e.printStackTrace();
        }

        Pay pay =  payService.getById(id);
        return ResultData.success(pay);
    }

    @Operation(summary = "查询全部流水",description = "查询支付流水方法")
    @GetMapping("/pay/getAll")
    public ResultData<List<Pay>> getAll(){
        List<Pay> list = payService.getAll();
        return  ResultData.success(list);
    }

    @Value("${server.port}")
    private String port;
    @GetMapping(value = "/get/info")
    public String getInfoByConsul(@Value("${atgugu.info}") String atgugu)
    {
        return "atgugu: "+atgugu+"port:"+port;
    }

}
